/**
 * 
 */
package com.wegax.finprod.accountcard;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.vaadin.ui.Component;
import com.vaadin.ui.Form;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.TextField;
import com.wegax.finprod.message.MessageCodes;
import com.wegax.finprod.message.Messages;
import com.wegax.finprod.validator.ValidatorsFactory;
import com.wegax.finprod.wizardform.WizardForm;
import com.wegax.finprod.wizardform.WizardFormStep;

/**
 * @author wegax
 * 
 */
class PersonalInformationWizardStep extends FormLayout implements WizardFormStep<PersonalAccountDebitCardMapper> {

	private static final Logger log = LoggerFactory.getLogger(PersonalInformationWizardStep.class);

	private static final String NAME_FIELD = "name";

	private static final String SURNAME_FIELD = "surname";

	private static final String PESEL_FIELD = "pesel";

	private static final String DOCUMENT_IDENTITY_FIELD = "identityCard";

	private WizardForm<PersonalAccountDebitCardMapper> wizardForm;

	private TextField nameTf;

	private TextField surnameTf;

	private TextField peselTf;

	private TextField identityCardTf;

	private Form form;

	public PersonalInformationWizardStep() {
		initComponents();
	}

	private void initComponents() {
		form = new Form();
		addComponent(form);

		nameTf = new TextField(Messages.getString(MessageCodes.NAME_FIELD_CAPTION));
		nameTf.setRequired(true);
		nameTf.setRequiredError(Messages.getString(MessageCodes.NAME_FIELD_REQUIRED_MESSAGE));
		form.addField(NAME_FIELD, nameTf);

		surnameTf = new TextField(Messages.getString(MessageCodes.SURNAME_FIELD_CAPTION));
		surnameTf.setRequired(true);
		surnameTf.setRequiredError(Messages.getString(MessageCodes.SURNAME_FIELD_REQUIRED_MESSAGE));
		form.addField(SURNAME_FIELD, surnameTf);

		peselTf = new TextField(Messages.getString(MessageCodes.PESEL_FIELD_CAPTION));
		peselTf.setRequired(true);
		peselTf.setRequiredError(Messages.getString(MessageCodes.PESEL_FIELD_REQUIRED_MESSAGE));
		peselTf.addValidator(ValidatorsFactory.peselValidator());
		form.addField(PESEL_FIELD, peselTf);

		identityCardTf = new TextField(Messages.getString(MessageCodes.IDENTITY_CARD_FIELD_CAPTION));
		identityCardTf.setRequired(true);
		identityCardTf.setRequiredError(Messages.getString(MessageCodes.IDENTITY_CARD_FIELD_REQUIRED_MESSAGE));
		identityCardTf.addValidator(ValidatorsFactory.identityCardValidator(Messages
				.getString(MessageCodes.IDENTITY_CARD_FIELD_INCORRECT_MESSAGE)));
		form.addField(DOCUMENT_IDENTITY_FIELD, identityCardTf);

		setDefaultValues();
	}

	private void setDefaultValues() {
		nameTf.setValue("John");
		surnameTf.setValue("Smith");
		peselTf.setValue("41011981759");
		identityCardTf.setValue("AVT559007");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.vaadin.teemu.wizards.WizardStep#getCaption()
	 */
	@Override
	public String getCaption() {
		return Messages.getString(MessageCodes.PERSONAL_INFORMATION_TITLE);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.vaadin.teemu.wizards.WizardStep#getContent()
	 */
	@Override
	public Component getContent() {
		return this;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.vaadin.teemu.wizards.WizardStep#onAdvance()
	 */
	@Override
	public boolean onAdvance() {
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.vaadin.teemu.wizards.WizardStep#onBack()
	 */
	@Override
	public boolean onBack() {
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.wegax.finprod.wizardform.WizardFormStep#getForm()
	 */
	@Override
	public Form getForm() {
		return form;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.wegax.finprod.wizardform.WizardFormStep#map(java.lang.Object)
	 */
	@Override
	public void map(PersonalAccountDebitCardMapper bean) {
		bean.setName((String) nameTf.getValue());
		bean.setSurname((String) surnameTf.getValue());
		bean.setPesel((String) peselTf.getValue());
		bean.setIdentityCard((String) identityCardTf.getValue());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.wegax.finprod.wizardform.WizardFormStep#initFormStep(com.wegax.finprod.wizardform.WizardForm)
	 */
	@Override
	public void initFormStep(WizardForm<PersonalAccountDebitCardMapper> wizardForm) {
		this.wizardForm = wizardForm;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.wegax.finprod.wizardform.WizardFormStep#onShow()
	 */
	@Override
	public void onShow() {

	}

}
